<?php

namespace App\Http\Controllers\Wxapi\newWork;

use App\Library\WxPay;
use App\Models\Admin\Configs;
use Illuminate\Support\Facades\DB;

class PayController extends CommonController
{
    private $workReplyTableName = "new_work_reply";
    private $workPayTableName = "new_work_pay_record";
    public function __construct()
    {
        parent::__construct();
    }

    public function applyPay(){
        if ($this->result['status'] > 0){
            return $this->return_result($this->result);
        }
        $param = request()->post();
        if(empty($param["id"])){
            $this->result['status'] = 1;
            $this->result['msg'] = '回复ID不能为空';
            return $this->return_result($this->result);
        }
        if(empty($param["url"])){
            $this->result['status'] = 1;
            $this->result['msg'] = '文件地址不能为空';
            return $this->return_result($this->result);
        }
        $replyData = DB::table($this->workReplyTableName)->where("id", $param["id"])->select("list_id", "annex")->first();
        if(empty($replyData->list_id)){
            $this->result['status'] = 1;
            $this->result['msg'] = '数据错误';
            return $this->return_result($this->result);
        }
        $annex = @json_decode($replyData->annex, true);
        if(empty($annex)){
            $this->result['status'] = 1;
            $this->result['msg'] = '附件错误';
            return $this->return_result($this->result);
        }
        $url = "";
        $money = 0;
        foreach ($annex as $item){
            if(isset($item["url"]) && $item["url"] == trim($param["url"])) {
                $money = isset($item["cost"]) ? $item["cost"] : 0;
                $url = $item["url"];
            }
        }
        if(floatval($money) <= 0){
            $this->result['status'] = 1;
            $this->result['msg'] = '金额错误';
            return $this->return_result($this->result);
        }
        $payData = [
            "total_fee" => $money * 100,
            "openid" => $this->user["openid"],
            "out_trade_no" => date('YmdHis') . rand(1000, 9999),
            "body" => "附件支付"
        ];
        $recordData = DB::table($this->workPayTableName)
            ->where("key", md5($param["id"] . $url))->where("user_id", $this->user["id"])
            ->select("status", "id")->first();
        if(empty($recordData->id)){
            $res = DB::table($this->workPayTableName)->insert([
                "order_sn" => $payData["out_trade_no"], "key" => md5($param["id"] . $url), "user_id" => $this->user["id"], "mall_id" => request()->mallData["id"],
                "list_id" => $replyData->list_id, "money" => $money, "status" => "0", "create_time" => date("Y-m-d H:i:s")
            ]);
        }else{
            $res = DB::table($this->workPayTableName)->where("id", $recordData->id)->update([
                "order_sn" => $payData["out_trade_no"], "money" => $money, "status" => "0", "create_time" => date("Y-m-d H:i:s")
            ]);
        }
        if($res){
            $configData = Configs::query()->where("mall_id", request()->mallData["id"])->first();
            $payObject = new WxPay($configData->member_wechat_appid, $configData->qy_mch_id, $configData->qy_mch_key);
            global $scf_data;
            if (isset($scf_data["IS_SCF"]) && $scf_data["IS_SCF"] == true){
                $payData["notifyUrl"] = $scf_data['host'] . "/wxapi/newWork/wxNotify"; //通知地址  确保外网能正常访问
            }else{
                $payData["notifyUrl"] = $_SERVER["REQUEST_SCHEME"] . '://'.$_SERVER['HTTP_HOST'] . "/wxapi/newWork/wxNotify"; //通知地址  确保外网能正常访问
            }
            if($data = $payObject->toPay($payData, $msg)){
                $this->result['data'] = $data;
                return $this->return_result($this->result);
            }else{
                $this->result['status'] = 1;
                $this->result['msg'] = $msg;
                return $this->return_result($this->result);
            }
        }else{
            $this->result['status'] = 1;
            $this->result['msg'] = '记录支付信息错误';
            return $this->return_result($this->result);
        }
    }
}
